package com.mpsc.ribbon.consumer.controller;

import com.mpsc.ribbon.consumer.config.LoadBalancer;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.cloud.client.ServiceInstance;
import org.springframework.cloud.client.discovery.DiscoveryClient;
import org.springframework.web.bind.annotation.RestController;
import org.springframework.web.client.RestTemplate;

import java.net.URI;
import java.util.List;

@RestController
public class RibbonController {

    @Autowired
    private DiscoveryClient discoveryClient;

    @Autowired
    private LoadBalancer loadBalancer;

    @Autowired
    private RestTemplate restTemplate;

    public String getPaymentLb() {

        List<ServiceInstance> instanceList = discoveryClient.getInstances("");

        if (instanceList == null || instanceList.size() <= 0) {
            return null;
        }
        ServiceInstance serviceInstance = loadBalancer.instance(instanceList);
        URI uri = serviceInstance.getUri();

        return  "";//restTemplate.postForEntity(uri+"/",Object.class);
    }
}
